package service.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaQuery;

import model.Film;

/**
 * Implementation for the custom repository functionality declared in
 * {@link DatentraegerRepositoryFilmVerwaltung} based on JPA. To use this
 * implementation in combination with Spring Data JPA you can either register it
 * programatically:
 * 
 * <pre>
 * EntityManager em = ... // Obtain EntityManager
 * 
 * UserRepositoryCustom custom = new UserRepositoryImpl();
 * custom.setEntityManager(em);
 * 
 * RepositoryFactorySupport factory = new JpaRepositoryFactory(em);
 * UserRepository repository = factory.getRepository(UserRepository.class, custom);
 * </pre>
 * 
 * Using the Spring namespace the implementation will just get picked up due to
 * the classpath scanning for implementations with the {@code Impl} postfix.
 * 
 * <pre>
 * &lt;jpa:repositories base-package=&quot;com.acme.repository&quot; /&gt;
 * </pre>
 * 
 * If you need to manually configure the custom instance see
 * {@link DatentraegerRepositoryJdbcImpl} for an example.
 * 
 * @author Andre
 */
class FilmRepositoryImpl implements FilmRepositoryDatentraeger {

	@PersistenceContext
	private EntityManager em;

	/**
	 * Configure the entity manager to be used.
	 * 
	 * @param em
	 *            the {@link EntityManager} to set.
	 */
	public void setEntityManager(EntityManager em) {

		this.em = em;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.jpa.sample.repository.UserRepositoryCustom#
	 * myCustomBatchOperation()
	 */
	public List<Film> datentraegerBatchOperation() {

		CriteriaQuery<Film> criteriaQuery = em.getCriteriaBuilder()
				.createQuery(Film.class);

		return em.createQuery(criteriaQuery).getResultList();
	}
}
